package com.kzd.sdk.ui.dialog;

import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentTransaction;

import com.kzd.sdk.callback.KZD_OnClickListener;
import com.kzd.sdk.ui.fragment.KZD_AccessEnrollFragment;
import com.kzd.sdk.ui.fragment.KZD_AccessForgetFragment;
import com.kzd.sdk.ui.fragment.KZD_AccessLoginFragment;
import com.kzd.sdk.utils.AppUtils;

import java.util.Objects;

public class KZD_AccessPortDialog extends DialogFragment implements KZD_OnClickListener {

    public final int frameLayoutId = 0x0000001;

    private Application mApplication;

    private Context mContext;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        this.mContext = context;
    }


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable  Bundle savedInstanceState) {
        Objects.requireNonNull(getDialog()).requestWindowFeature(Window.FEATURE_NO_TITLE);
        FrameLayout frameLayout = new FrameLayout(mContext);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams((-1), (-2));
        frameLayout.setLayoutParams(params);
        frameLayout.setId(frameLayoutId);
        return frameLayout;
    }

//    @Override
//    public void onViewCreated(View view, Bundle savedInstanceState) {
//        super.onViewCreated(view, savedInstanceState);
//        mApplication = mActivity.getApplication();
//        AppUtils.setCustomDensity((this), mApplication);
//
//        showLoginFragment();
//    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mApplication = getActivity().getApplication();

//        AppUtils.setCustomDensity((this), mApplication);

        showLoginFragment();
    }

    @Override
    public void onStart() {
        WindowManager.LayoutParams params = Objects.requireNonNull(getDialog().getWindow()).getAttributes();
        if (mApplication.getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_PORTRAIT) {
            params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        } else {
            params.width = AppUtils.dp2px(360);
        }
        getDialog().getWindow().setAttributes(params);

        getDialog().getWindow().getDecorView()
                .setBackgroundColor(Color.TRANSPARENT);

        if (mApplication.getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_PORTRAIT) {
            getDialog().getWindow().getDecorView()
                    .setPadding(AppUtils.dp2px((30)), (0), AppUtils.dp2px((30)), (0));
        } else {
            getDialog().getWindow().getDecorView()
                    .setPadding((0), AppUtils.dp2px((15)), (0), AppUtils.dp2px((15)));
        }
        getDialog().setCanceledOnTouchOutside(false);
        getDialog().setOnKeyListener((dialog, keyCode, event) -> {
            int count = getChildFragmentManager().getBackStackEntryCount();
            if (keyCode != KeyEvent.KEYCODE_BACK || count <= 0) {
                return false;
            }
            popBackStack();
            return true;
        });
        super.onStart();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == AppUtils.getId(mContext, "tvAccessLoginRegister")) {
            showEnrollFragment();
        } else if (v.getId() == AppUtils.getId(mContext, "tvAccessLoginForget")) {
            showForgetFragment();
        } else if (v.getId() == AppUtils.getId(mContext, "ivAccessEnrolBack")) {
            popBackStack();
        } else if (v.getId() == AppUtils.getId(mContext, "ivAccessForgetBack")) {
            popBackStack();
        }
    }

    /**
     * fragment 回退
     */
    private void popBackStack() {
        getChildFragmentManager().popBackStack();
    }

    /**
     * 显示用户登录视图
     */
    private void showLoginFragment() {
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        KZD_AccessLoginFragment fragment = new KZD_AccessLoginFragment();
        fragment.setOnClickListener(this);
        transaction.replace(frameLayoutId, fragment);
        transaction.commit();
    }

    /**
     * 显示用户注册视图
     */
    private void showEnrollFragment() {
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        KZD_AccessEnrollFragment fragment = new KZD_AccessEnrollFragment();
        fragment.setOnClickListener(this);
        transaction.add(frameLayoutId, fragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    /**
     * 显示忘记密码视图
     */
    private void showForgetFragment() {
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        KZD_AccessForgetFragment fragment = new KZD_AccessForgetFragment();
        fragment.setOnClickListener(this);
        transaction.add(frameLayoutId, fragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

}
